<?php

/*
 * // +----------------------------------------------------------------------
 * // | erp
 * // +----------------------------------------------------------------------
 * // | Copyright (c) 2006~2020 erp All rights reserved.
 * // +----------------------------------------------------------------------
 * // | Licensed ( LICENSE-1.0.0 )
 * // +----------------------------------------------------------------------
 * // | Author: yxx <1365831278@qq.com>
 * // +----------------------------------------------------------------------
 */

namespace App\Admin\Metrics;

use App\Models\Project;
use Dcat\Admin\Widgets\Metrics\Round;
use Illuminate\Http\Request;

class ProjectOrders extends Round
{
    /**
     * 初始化卡片内容
     */
    protected function init()
    {
        parent::init();

        $this->title('项目执行状态');
        $this->chartLabels(['已完结', '待完结']);
        // $this->dropdown([
        //     '7' => 'Last 7 Days',
        //     '28' => 'Last 28 Days',
        //     '30' => 'Last Month',
        //     '365' => 'Last Year',
        // ]);
    }

    /**
     * 处理请求
     *
     * @param Request $request
     *
     * @return mixed|void
     */
    public function handle(Request $request)
    {
        // 卡片内容
        $project = Project::all();
        // 项目总计
        $total = $project->count();
        // 已完成
        $finished = $project->where('status', 1)->count();
        // 执行中
        $pending = $project->where('status', 0)->count();
        if ($finished != 0 && $pending != 0) {
            $proportion = [ceil(($finished / $total) * 100), ceil(($pending / $total) * 100)];
        } else {
            $proportion = [];
        }


        $this->withContent($finished, $pending);
        // 占比
        // 图表数据
        $this->withChart($proportion);
        // 总数
        $this->chartTotal('总计', $total);
    }

    /**
     * 设置图表数据.
     *
     * @param array $data
     *
     * @return $this
     */
    public function withChart(array $data)
    {
        return $this->chart([
            'series' => $data,
        ]);
    }

    /**
     * 卡片内容.
     *
     * @param int $finished
     * @param int $pending
     * @param int $rejected
     *
     * @return $this
     */
    public function withContent($finished, $pending)
    {
        return $this->content(
            <<<HTML
<div class="col-12 d-flex flex-column flex-wrap text-center" style="max-width: 220px">
    <div class="chart-info d-flex justify-content-between mb-1 mt-2" >
          <div class="series-info d-flex align-items-center">
              <i class="fa fa-circle-o text-bold-700 text-primary"></i>
              <span class="text-bold-600 ml-50">已完结</span>
          </div>
          <div class="product-result">
              <span>{$finished}</span>
          </div>
    </div>

    <div class="chart-info d-flex justify-content-between mb-1">
          <div class="series-info d-flex align-items-center">
              <i class="fa fa-circle-o text-bold-700 text-warning"></i>
              <span class="text-bold-600 ml-50">待完结</span>
          </div>
          <div class="product-result">
              <span>{$pending}</span>
          </div>
    </div>


</div>
HTML
        );
    }
}
